<?php
/**
 * Created by PhpStorm.
 * User: holy
 * Date: 2017/10/25
 * Time: 上午11:32
 */

namespace Admin\Controller;
use Think\Controller;

class UserController extends Controller
{
//    public function register()
//    {
//        /***
//         * 加载注册页面
//         */
//        $User = M('User');
//        if (IS_POST) {
//            $data['username'] = trim(I('post.username'));
//            if ($data['username'] == '') {
//                $this->error("请填写用户名");
//            }
//            //查询数据库里面是否存在此用户
//            $user = $User->where($data)->find();
//            if ($user) {
//                $this->error('此用户名已被注册');
//            }
//            if (strlen($data['username']) < 5 || strlen($data['username']) > 20) {
//                $this->error("用户名长度不能小于5位并且不能大于20位");
//            }
//            $data['password'] = set_password(I('post.password'));
//            if ($data['password'] == '') {
//                $this->error("请填写密码");
//            }
//            /***
//             * 验证 两次密码是否一致
//             */
//            $check_password = I('post.password');
//            if (!$check_password = I('post.password')) {
//                $this->error("两次输入密码不一致");
//            }
//            $data['email'] = I('post.email');
//            $User->add($data);
//            $this->success("恭喜你，注册成功", U('User/login'));
//        } else {
//            $this->display();
//        }
//    }

    /***
     *AJAX 注册
     */
    public function ajax_register(){
        $User = M('User');
        if(IS_AJAX){
            $data['username'] = trim(I('post.username'));
            $data['password'] = set_password(I('post.password'));
            $data['mobile'] = I('post.mobile');
            $data['email'] = I('post.email');
            $check_password = I('post.check_password');
            //验证邮箱
            $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
            //验证手机号
            $telphone = "#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#";
            //验证唯一

            $info = array('status' =>1, 'msg' =>'注册成功');
            $username = $User->where("username='$data[username]'")->find();
            $email = $User->where("email='$data[email]'")->find();
            $mobile = $User->where("mobile='$data[mobile]'")->find();
            if($username){
                $info = array('status' =>0, 'msg' =>'用户名已被注册');
            }
            else if($email){
                $info = array('status' =>0, 'msg' =>'邮箱已被注册');
            }
            else if($mobile){
                $info = array('status' =>0, 'msg' =>'手机号已被注册');
            }
            else if(strlen($data['username'])<6){
                $info = array('status' =>0, 'msg' =>'用户名长度不能少于6位');
            }
            else if (!preg_match( $pattern, $data['email'] ) ){
                $info = array('status' =>0, 'msg' =>'请填写正确邮箱格式');
            }
            else if (!preg_match( $telphone, $data['mobile'] ) ){
                $info = array('status' =>0, 'msg' =>'请填写正确手机号');
            }
            else if(strlen(I('post.password'))<6){
                $info = array('status' =>0, 'msg' =>'密码长度不能少于6位');
            }
            else if($check_password!=I('post.password')){
                $info = array('status' =>0, 'msg' =>'两次密码填写不一致');
            }
            if($info['status']==1){
                $User->create();
                $User->add($data);
            }
            $this->ajaxReturn($info);
        }else{
            $this->display();
        }
    }
    /***
     * ajax 登录
     */
    public function ajax_login(){
        $User = M('User');
        if(IS_AJAX){
            $map['username|email|mobile'] = trim(I('post.username'));
            $map['password'] = set_password(I('post.password'));
            $code = I('post.code');

            $user =  $User->where($map)->find();

            if(!$user){
                $info = array('status'=>0,'msg'=>"用户名或密码错误");
            }else if(!$this->check_verify($code)){
                $info = array('status'=>0,'msg'=>"验证码输入错误");
            }else {
                 $_SESSION['user'] = $user;
                $info = array('status'=>1,'msg'=>"登录成功");
            }
            $this->ajaxReturn($info);
        }
    }



//    /***
//     * 加载登录页面
//     */
//    public function login()
//    {
//        $User = M('User');
//        if (IS_POST) {
//            $data['username'] = trim(I('post.username'));
//            $data['password'] = set_password(I('post.password'));
//
//            $code = I('post.code');
//            if ($code == '') {
//                $this->error('请填写验证码');
//            }
//            if (!$this->check_verify($code)) {
//                $this->error('验证码填写错误');
//            }
//
//            //查询数据库里面是否存在此用户
//            $user = $User->where($data)->find();
//            if (!$user) {
//                $this->error('用户名或密码输入错误');
//            }
//            $_SESSION['user'] = $user;
//            $this->success('恭喜您，登录成功', U('Index/index'));
//        } else {
//            $this->display();
//        }
//    }

    /***
     * 退出登录
     */
    public function logout()
    {
        //Session的销毁
        $_SESSION = array();
        //删除sessin后, 再清除对应的cookie
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), "", time() - 3600, "/");
        }
        session_destroy();
        $this->success('退出成功', U('login'));
    }


    /**
     * 验证码
     */
    function verify()
    {
        $config = array(
            'fontSize' => 30,    // 验证码字体大小
            'length' => 4,     // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
            'codeSet' => '0123456789' //纯数字验证
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    /***
     *密码重置页面
     */
    public function password_reset()
    {
        $this->display();
    }

    /***
     * 找回密码
     */
    public function findpwd()
    {
        $map['email'] = $_POST['email'];
        $token = make_token();  //生成token
        M('User')->where($map)->setField('token', $token);
        $info = M('User')->where($map)->find();
//		dump($info);exit;
        if ($info) {
            $url = $_SERVER ['HTTP_HOST'] . U('User/email') . '?token=' . $info['token'];
            // 调用发送邮件函数
            $username = $info ['username'];
            $title = "找回密码";

            $content = "<h3>亲爱的：$username 用户</h3>
            <br><br>http://$url
			<br><br><br><h4>有效期30分钟</h4>
			<br><br>
			<img src='__PUBLIC__/images/qq.png'>";

            $from = "931922545@qq.com"; //修改为你的发送邮箱
            $to = $info ['email'];
            $status = send_mail($title, $content, $from, $to);
            if ($status == 1) {
                $this->success('发送邮件成功,请登录邮箱重设密码~', U('User/edit_pwd'), 2);
            } else {
                $this->error('发送邮件失败...');
                exit ();
            }

        } else {
            $this->error('此邮箱未注册');
            exit ();
        }
    }

    /**
     * 修改密码
     */
    public function edit_pwd()
    {

        $Customer = M('Customer');
        if (IS_POST) {
            $token = $_SESSION['user']['token'];
            $data['password'] = set_password(I('post.password'));
            $check_password = I('post.check_password');

            //判断密码是否为空
            if (I('post.password') == "") {
                $this->error('密码不能为空');
            }
            //判断密码的长度不能小于6位
            if (strlen(I('post.password')) < 6) {
                $this->error('密码的长度不能小于6位');
            }
            //判断两次输入的密码是否一致
            if ($check_password != I('post.password')) {
                $this->error('两次输入的密码不一样，请重新输入');
            }

            $Customer->where("token = '$token'")->save();
            $this->success('修改密码成功', U('login'));

        } else {
            $this->display();
        }
    }
}